#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int N = 30010;

int p[N];
int d[N];
int w[N];

int find(int x){
    if(x != p[x]){
        int root = find(p[x]);
        d[x] += d[p[x]];
        p[x] = root;
    }
    return p[x];
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    for(int i = 1; i < N; i ++) p[i] = i, w[i] = 1;
    int T;
    cin >> T;
    while(T--){
        char op[2];
        int a, b;
        cin >> op >> a >> b;
        if(op[0] == 'M'){
            int pa = find(a), pb = find(b);
            if(pa != pb){
                d[pa] = w[pb];
                w[pb] += w[pa];
                p[pa] = pb;
            }
        }else{
            int pa = find(a), pb = find(b);

            if(pa != pb){
                cout << -1 << endl;
            }else{
                cout << max(abs(d[a] - d[b]) - 1, 0) << endl;
            }
        }
    }

    return 0;
}